package com.yanqu.road.server.cmd.activity.rank;

import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.pb.activity.CrossActivityRankProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = CrossProtocol.C2_CROSS_GET_TRADE_WAR_ACTIVITY_RANK, desc = "查看跨服商战榜单")
public class GetCrossTradeWarRankListCmd extends CrossCommand {
    private long userId;
    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityRankProto.CrossActivityRankReqMsg reqMsg = CrossActivityRankProto.CrossActivityRankReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossActivityRankListModel rankListModel = CrossTradeWarRankMgr.getCrossActivityRankListModel(activityId);
        CommonActivityProto.CommonActivityRankListRespMsg.Builder respMsg = CommonActivityProto.CommonActivityRankListRespMsg.newBuilder();
        respMsg.setMyRank(-1);
        if (rankListModel != null) {
            RankProto.RankListMsg.Builder rankListMsg = rankListModel.getRankListMsg();
            RankProto.RankListMsg.Builder tempMsg = RankProto.RankListMsg.newBuilder();
            int num = 0;
            for (RankProto.RankTempMsg msg : rankListMsg.getRankListList()) {
                tempMsg.addRankList(msg);
                num++;
                if (num >= ActivityMgr.getRankListShowNum()) {
                    break;
                }
            }
            respMsg.setRankList(tempMsg);
            respMsg.setMyRank(rankListModel.getMyRank(userId, serverId));
        }else {
            respMsg.setRankList(RankProto.RankListMsg.newBuilder());
        }
        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_TRADE_WAR_ACTIVITY_RANK, respMsg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
